package com.yjhb.android.sbz.activity

import com.yjhb.android.sbz.BuildConfig
import com.yjhb.android.sbz.R
import com.yjhb.android.sbz.tool.DataSaveTool
import com.yjhb.android.sbz.tool.Tool
import com.jojo.android.zxlib.base.mvp.BasePresenter
import com.jojo.android.zxlib.tool.ZxLogTool
import com.jojo.android.zxlib.tool.ZxToast
import kotlinx.android.synthetic.main.activity_console.*

/**
 * 创建者 zx
 * 创建时间 2021/3/8 11:36
 */
class ConsoleActivity : BaseActivity<BasePresenter<*, *>?>() {
    private val TAG = "ConsoleActivity"

    override fun isImmerse() = false

    override fun initPresenter(): BasePresenter<*, *>? = null

    override fun getLayoutId() = R.layout.activity_console

    override fun initUI() {
    }

    override fun initData() {}

    override fun regEvent() {
        console_et.setOnEditorActionListener { _, _, _ ->
            try {
                val all = console_et.text.toString()
                val lines = all.split("\n")
                if (lines.isNotEmpty()) {
                    val lastLine = lines[lines.size - 1]
                    val pars = lastLine.split(" ")
                    command(pars)
                }
            } catch (e: Exception) {
                console_et.setText(StringBuilder(console_et.text.toString())
                        .append(e.toString())
                )
            }
            false
        }
    }


    private fun command(args: List<String>) {
        if (args.isNotEmpty()) {
            ZxLogTool.i("args:${args}")
            when (args[0]) {
                "go" -> if (args[1] == "c") {
                    val id = args[2]
                    val name = if (args.size > 3) args[3] else ""
                    val i = if (args.size > 4) args[4].toInt() else 0
                    val extra = if (args.size > 5) args[5] else null
                    Tool.startChatActivity(this, id, name, i, extra)
                }
                "host" -> {
                    when (args[1]) {
                        "1" -> {
                            DataSaveTool.saveHost(BuildConfig.ServerURL_TEST)
                            DataSaveTool.saveIM_APP_ID(BuildConfig.IM_APP_ID_TEST)
                        }
                        "2" -> {
                            DataSaveTool.saveHost(BuildConfig.ServerURL_ALPHA)
                            DataSaveTool.saveIM_APP_ID(BuildConfig.IM_APP_ID_RELEASE)
                        }

                        "3" -> {
                            DataSaveTool.saveHost(BuildConfig.ServerURL_RELEASE)
                            DataSaveTool.saveIM_APP_ID(BuildConfig.IM_APP_ID_RELEASE)
                        }
                        "4" -> {
                            DataSaveTool.saveHost("http://192.168.3.31:8000")
                            DataSaveTool.saveIM_APP_ID(BuildConfig.IM_APP_ID_TEST)
                        }
                        "5" ->{
                            DataSaveTool.saveHost(args[2])
                            DataSaveTool.saveIM_APP_ID(BuildConfig.IM_APP_ID_TEST)
                        }
                    }
                    ZxToast.showINFOBottomToast("服务器已切换：${DataSaveTool.getHost()}，重启生效！");
                }
            }
        }
    }
}